package zyf;

import java.util.ArrayList;
import java.util.List;

public class syTest {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();

        Thread t1 = new Thread() {
            public synchronized void run() {
                for (int i = 0; i < 100; i++) {
                    synchronized (list1){
                        list1.add(i);
                    }
                }
                System.out.println("添加完毕");
                System.out.println(list1.size());
            }
        };
            Thread t2 = new Thread() {
                public synchronized void run() {
                    for (int i = 0; i < 100; i++) {
                        synchronized (list1) {
                            list1.add(i);
                        }
                    }
                    System.out.println("添加完毕");

                }

            };

        t1.start();
        t2.start();


        }
    }

